#include "gamescene.h"

/******************/
extern int ball_skin;
/******************/

GameScene::GameScene(QWidget *parent) :
    QWidget(parent),
Ui::GameScene()
{
    setupUi(this);
    inite_game_scene();

    player_life = PLAYER_LIFE;
    loser_flag = false;
    stopped = false;

QTimer* hardbreak = new QTimer(this);
hardbreak->start(67);

connect(hardbreak,&QTimer::timeout,[=](){
for(int i = 0;i< brick_num;++i){
    if(enemies[i]->ishard == true)
        enemies[i]->ishard = false;
}
});

    QTimer* timer = new QTimer(this);

    timer->start(10);

    connect(timer,&QTimer::timeout,[=](){
          collisionDetection();
          emit repaintBall();

          if(m_ball.ball->y()>=870)
           {
              if(player_life!=1){
                  player_life-=1;
                  m_ball.inite_ball();
                  p1.inite_board();
              }
              else
              {
                 loser_flag = true;
                 player_life = PLAYER_LIFE;
              }
           }

         if(loser_flag)
         {
               this->close();
               emit defeat();
               loser_flag=false;
               m_ball.inite_ball();
               p1.inite_board();
               for(int i = 0;i<brick_num;++i)
               {
                   if(enemies[i]->is_son)
                       enemies[i]->life = 2;
                   enemies[i]->isLive=true;
                   stopped = false;
               }
         }
            if(loser_flag==false)
            {int count = 0;
                for(int i =0;i<brick_num;++i)
                {
                    if(enemies[i]->isLive==false)
                    count++;
                }
                   if(count==brick_num)
                   {
                       this->close();

                       emit victory();
                       m_ball.inite_ball();
                       p1.inite_board();
                       for(int i = 0;i<brick_num;++i)
                       {
                          if(enemies[i]->is_son)
                               enemies[i]->life = 2;

                               enemies[i]->isLive=true;
                       }
                        stopped = false;
                        player_life = PLAYER_LIFE;
                   }


                }


    });

    connect(this,&GameScene::timestop,[=](){ timer->stop();});
    connect(this,&GameScene::timestart,[=](){timer->start(10);});
    connect(this,&GameScene::move_player_right,this,&GameScene::paintright);
    connect(this,&GameScene::move_player_left,this,&GameScene::paintleft);
    connect(this,&GameScene::repaintBall,this,&GameScene::paintball);
    connect(this,&GameScene::set_has_shot,this,&GameScene::set_shot);

}

GameScene::~GameScene()
{

}

//初始化游戏界面
void GameScene::inite_game_scene()
{
    setFixedSize(SCENE_WIDTH,SCENE_HEIGHT);
    setWindowTitle(GAME_NAME);
    setWindowIcon(QIcon(":/sucai/ball.PNG"));



}

//从main收主菜单信号的槽，用来从主菜单跳转
void GameScene::receivestart()
{
    this->show();
}

//画画
void GameScene::paintEvent(QPaintEvent *)
{

    QPainter nt(this);
    nt.drawPixmap(0,0,900,900,QPixmap(":/sucai/blackback.png"));

    QPainter pt(this);
    if(ball_skin==1)
    {
    pt.drawPixmap(p1.getx(),p1.gety(),BOARD_LENGTH,BOARD_WIDTH,QPixmap(":/sucai/board1.jpg"));
    }
    else if(ball_skin ==2)
    {
    pt.drawPixmap(p1.getx(),p1.gety(),BOARD_LENGTH,BOARD_WIDTH,QPixmap(":/sucai/bamboo.png"));
    }
    else
    {
    pt.drawPixmap(p1.getx(),p1.gety(),BOARD_LENGTH,BOARD_WIDTH,QPixmap(":/sucai/iceboard.png"));
    }

    QPainter lifer(this);
    pt.drawPixmap(736,868,32,32,QPixmap(":/sucai/life.png"));

    QPainter count(this);
    if(player_life == 3)
     pt.drawPixmap(768,868,32,32,QPixmap(":/sucai/3.png"));
    if(player_life == 2)
     pt.drawPixmap(768,868,32,32,QPixmap(":/sucai/2.png"));
    if(player_life == 1)
     pt.drawPixmap(768,868,32,32,QPixmap(":/sucai/1.png"));

    QPainter bpt(this);
    int i = 0;

    for(;i<brick_num;++i)
    {
        if(enemies[i]->isLive)
        {
            if(enemies[i]->life==1)
            {
                bpt.drawPixmap(enemies[i]->bk.x(),enemies[i]->bk.y(),BRICK_LENGTH,BRICK_WIDTH,QPixmap(":/sucai/brick.PNG"));
            }


            else if(enemies[i]->life==2)
            {
                bpt.drawPixmap(enemies[i]->bk.x(),enemies[i]->bk.y(),BRICK_LENGTH,BRICK_WIDTH,QPixmap(":/sucai/hardbrick.png"));
            }
        }
    }

    for(int j=0;j<brick_num;++j)
      {
          if(m_booms[j].m_Free==false)
          {

              QPainter painter(this);
              painter.drawPixmap(m_booms[j].m_X,m_booms[j].m_Y,BRICK_LENGTH,BRICK_WIDTH,m_booms[j].m_pixArr[m_booms[j].m_index]);
              m_booms[j].updateInfo();

          }

      }

    if(ball_skin == 1)
    {
    QPainter ballpt(this);
    ballpt.drawPixmap(m_ball.x,m_ball.y,BALL_RADIUS*2,BALL_RADIUS*2,QPixmap(":/sucai/littleBall.PNG"));
    }

    if(ball_skin==2)
    {
    QPainter ballpt(this);
    ballpt.drawPixmap(m_ball.x,m_ball.y,BALL_RADIUS*2,BALL_RADIUS*2,QPixmap(":/sucai/panda.png"));
    }

    if(ball_skin==3)
    {
    QPainter ballpt(this);
    ballpt.drawPixmap(m_ball.x,m_ball.y,BALL_RADIUS*2,BALL_RADIUS*2,QPixmap(":/sucai/iceball.png"));
    }


    QPainter st(this);
    if(stopped == true)
    {
        st.drawPixmap(325,370,150,150,QPixmap(":/sucai/stop.png"));
    }
}

void GameScene::paintleft()
{
    update();
    p1.move_left();
     if(m_ball.has_shot==false)
     {
         m_ball.moveto(p1.getx()+60);
     }
    update();
}

void GameScene::paintright()
{
    update();
    p1.move_right();
     if(m_ball.has_shot==false)
     {
         m_ball.moveto(p1.getx()+60);
     }
     update();
}


void GameScene::paintball()
{
    if(m_ball.has_shot==true)
    {
        m_ball.move();
    }
    update();
}

void GameScene::set_shot()
{
    m_ball.has_shot=true;
}

void GameScene::collisionDetection()
{
   //撞边
    if(m_ball.y<=0||m_ball.y>=870)
    {
        m_ball.direct = -m_ball.direct;
        return;
    }
    if(m_ball.x>=770||m_ball.x<=0)
    {
        if(m_ball.direct >= 0 )
        {
            m_ball.direct = 180-m_ball.direct;
        }
        else
        {
            m_ball.direct = -(180+m_ball.direct);
        }
        return;
    }

    m_ball.switch_direct3(p1.player);

   int key1 = -1;
    for(int i = 0;i<brick_num;i++)
    {
        if(enemies[i]->isLive == false)
            continue;

       else if(enemies[i]->isLive == true)
       {
           key1 = i;
       }

  if(enemies[i]->ishard==false)
  {
   if(m_ball.m_intersects(enemies[key1]->bk.x(),enemies[key1]->bk.y()))//如果碰到了
     {

           m_ball.switch_direct_brick(enemies[key1]->bk.x(),enemies[key1]->bk.y());
     if(enemies[i]->ishard==false)
       {
           if(enemies[i]->life == 2)
           {
               enemies[i]->life-=1;

               enemies[i]->ishard = true;

           }
          else if(enemies[i]->life==1)
           {
           enemies[key1]->isLive = false;
           }
       }

      if(enemies[i]->isLive == false)
      {
             for(int k=0;k<brick_num;k++)
                 {
                    if(m_booms[k].m_Free)
                            {

                         m_booms[k].m_Free=false;
                         m_booms[k].m_X=enemies[key1]->get_brick_x();
                         m_booms[k].m_Y=enemies[key1]->get_brick_y();


                                  break;
                            }
                 }
       }
     }

    else
    {
     if(m_ball.m_intersects(enemies[key1]->bk.x(),enemies[key1]->bk.y()))
     {
//        m_ball.direct = -m_ball.direct;//要优化也是这里优化了
           m_ball.switch_direct_brick(enemies[key1]->bk.x(),enemies[key1]->bk.y());
     }
    }
   }
  }
}



void GameScene::keyPressEvent(QKeyEvent *event)
{
    if(event->key()==80)
    {
        if(stopped == false)
        {
            emit timestop();
            stopped = true;
            update();
        }
        else
        {
            emit timestart();
            stopped = false;
            update();
        }
    }

   if(stopped == false)
   {
    if(event->key()==16777234)//左箭头
    {
       emit move_player_left();
    }

    else if(event->key()==16777236)//右箭头
    {
       emit move_player_right();
    }
   }
}

void GameScene::keyReleaseEvent(QKeyEvent *event)
{
   if(stopped == false){//如果没暂停
   if(event->isAutoRepeat()){}

   else
    {

       if(event->key()==32)
           emit set_has_shot();
    }

 }
}
